Priority File Manager

📁 public_html
Base Directory:
/home/ecedu/public_html/cce/Controllers
NameTypeSizeActions
📁 .. Folder -
📄 MainController.php File 4796
Edit Download

View File: MainController.php

<?php
require_once __DIR__ . '/../Models/NewsModel.php';
require_once __DIR__ . '/../Models/Functions.php';

class MainController 
{
    protected $langData = [];
    protected $menuData = [];
    protected $newsModel;
    
    public function __construct()
    {
        $this->newsModel = new NewsModel();
    }

    /**
     * Load language data from JSON file
     * @param string $lang Language code (e.g., 'ar', 'en')
     */
    private function loadLanguage($lang)
    { 
        $file = __DIR__ . '/../Languages/lang.json'; 
        if (!file_exists($file)) {
            // Log error or set default language data
            $this->langData = [];
            return;
        }
        
        $json = file_get_contents($file);
        $allLangs = json_decode($json, true);
        
        // Fallback to 'ar' if language is not found
        $this->langData = isset($allLangs[$lang]) ? $allLangs[$lang] : ($allLangs['ar'] ?? []); 
    }
    
    private function loadMenu()
    {  
        $file = __DIR__ . '/../Languages/menu.json'; 
        if (!file_exists($file)) {
            // Log error or set default language data
            $this->menuData = [];
            return;
        }

        $json = file_get_contents($file);
        $allMenu = json_decode($json, true);
        
        // Fallback to 'ar' if language is not found
        $this->menuData = $allMenu; 
    }
    
    /**
     * Get translation for a key
     * @param string $key Translation key
     * @return string Translated string or key if not found
     */
    public function __($key)
    {
        return $this->langData[$key] ?? $key;
    }
     
    public function getNewsByType(int $type): ?array
    {
      return  $this->newsModel->getNewsByType($type);
    }
    
    public function getNewsById(int $type,int $id): ?array
    {
      return  $this->newsModel->getNewsById($type,$id);
    }
    
    public function increaseViewCount(int $type,int $id)
    {
         $this->newsModel->increaseViewCount($type,$id);
    }

    /**
     * Render a view with data
     * @param string $view View file name without .php
     * @param array $data Data to pass to the view
     */
    protected function render($view,$i, $data = [])
    {
        extract($data);
        $content = __DIR__ . '/../Views/layouts/' . $view . '.php';  
        require __DIR__ . '/../Views/layouts/main-layout.php'; 
    }
   
    public function index($i = 1, $lang = 'ar')
    {
    $this->loadMenu();
    $this->loadLanguage($lang);

    $limit = 12;
    $newsByType = [];
    $mostReadNews = [];
     
    $newsTypes = $this->newsModel->getAllTableNames();  
     
    foreach ($newsTypes as $typeId => $tableName) {
        $newsByType["newsType$typeId"] = $this->newsModel->getNewsByType($typeId, $limit); 
    }
     
    $newsMap = [];
    foreach ($newsByType as $typeKey => $newsList) {
        foreach ($newsList as $newsItem) {
            $key = $newsItem['news_id'] . '-' . str_replace('newsType', '', $typeKey);
            $newsMap[$key] = $newsItem;
        }
    }
     
    $readView = $this->newsModel->getViewCount($limit); 
       
    foreach ($readView as $row) {
     $key = trim((string)$row['news_id']) . '-' . trim((string)$row['table_id']);  
     if (array_key_exists($key, $newsMap)) { 
        $item = $newsMap[$key];
        $item['num_view'] = $row['total_views'] ?? $row['num_view'] ?? 0;
        $item['table_id'] = $row['table_id'];
        $mostReadNews[] = $item;
     } 
    }



    $data = [
        'L' => $this->langData,
        'M' => $this->menuData,
        'sliderNews' => array_slice($newsByType['newsType1'] ?? [], 0, 12),
        'lastNews' => array_slice($newsByType['newsType1'] ?? [], 0, 6),
        'eventsNews' => array_slice($newsByType['newsType6'] ?? [], 0, 3),
        'mostReadNews' => $mostReadNews ,
        'editorialData' => $this->newsModel->getBasic('dbs_about_us'),
        'contactusData' => $this->newsModel->getBasic('dbs_contact_us'),
        'socialData' => $this->newsModel->listSocials(),
        'imageData' => $this->newsModel->listImages(),
        'adsData' => $this->newsModel->listAds(),
    ];
     
    for ($n = 1; $n <=  $this->newsModel->getMaxTables(); $n++) {
        $data["newsType$n"] = $newsByType["newsType$n"] ?? [];
    }

    $layoutMap = [
        2 => 'article-layout',
        3 => 'aboutus-layout',
        4 => 'contactus-layout',
        5 => 'list-news-layout',
        6 => 'department-layout',
        7 => 'search-layout',
        8 => 'list-pdf-layout',
    ];

    $layout = $layoutMap[$i] ?? 'index-layout';
    $this->render($layout, $i, $data);
 } 
}